package cn.iocoder.yudao.module.shop.controller.admin.store;

import cn.iocoder.yudao.module.shop.dal.mysql.store.StoreMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.store.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.store.StoreDO;
import cn.iocoder.yudao.module.shop.convert.store.StoreConvert;
import cn.iocoder.yudao.module.shop.service.store.StoreService;

@Tag(name = "管理后台 - 库存")
@RestController
@RequestMapping("/shop/store")
@Validated
public class StoreController {

    @Resource
    private StoreService storeService;

    @PostMapping("/create")
    @Operation(summary = "创建库存")
    @PreAuthorize("@ss.hasPermission('shop:store:create')")
    public CommonResult<Long> createStore(@Valid @RequestBody StoreCreateReqVO createReqVO) {
        return success(storeService.createStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存")
    @PreAuthorize("@ss.hasPermission('shop:store:update')")
    public CommonResult<Boolean> updateStore(@Valid @RequestBody StoreUpdateReqVO updateReqVO) {
        storeService.updateStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:store:delete')")
    public CommonResult<Boolean> deleteStore(@RequestParam("id") Long id) {
        storeService.deleteStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:store:query')")
    public CommonResult<StoreRespVO> getStore(@RequestParam("id") Long id) {
        StoreDO store = storeService.getStore(id);
        return success(StoreConvert.INSTANCE.convert(store));
    }

    @PostMapping("/code")
    @Operation(summary = "通过条码获得库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:store:query')")
    public CommonResult<StoreRespVO> getStoreByCode(@Valid @RequestBody StoreReqVO reqVO) {
        StoreDO store = storeService.getStoreByCode(reqVO.getCode(), reqVO.getTenantId());
        return success(StoreConvert.INSTANCE.convert(store));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库存列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:store:query')")
    public CommonResult<List<StoreRespVO>> getStoreList(@RequestParam("ids") Collection<Long> ids) {
        List<StoreDO> list = storeService.getStoreList(ids);
        return success(StoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/all")
    @Operation(summary = "获得库存列表")
    @PreAuthorize("@ss.hasPermission('shop:store:query')")
    public CommonResult<List<StoreRespVO>> getAllList() {
        List<StoreDO> list = storeService.getAllList();
        return success(StoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存分页")
    @PreAuthorize("@ss.hasPermission('shop:store:query')")
    public CommonResult<PageResult<StoreRespVO>> getStorePage(@Valid StorePageReqVO pageVO) {
        PageResult<StoreDO> pageResult = storeService.getStorePage(pageVO);
        return success(StoreConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/outstore")
    @Operation(summary = "手动出库")
    @PreAuthorize("@ss.hasPermission('shop:store:update')")
    public CommonResult<Boolean> outStore(@Valid @RequestBody StoreOutReqVO reqVO){
        storeService.outStore(reqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存 Excel")
    @PreAuthorize("@ss.hasPermission('shop:store:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreExcel(@Valid StoreExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StoreDO> list = storeService.getStoreList(exportReqVO);
        // 导出 Excel
        List<StoreExcelVO> datas = StoreConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库存.xls", "数据", StoreExcelVO.class, datas);
    }

}
